<template>
  <div>
    <h1>统计</h1>
    <div id="rent"></div>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    return {
      //存储差评数据
      negative: [],
      //存储未评价的数据
      noComment: [],
      //存储好评数据
      goodComment: [],
      //存储差评数组的长度
      negativeLength: 0,
      //未评数组长度
      noCommentLength: 0,
      //好评数组长度
      goodCommentLength: 0
    };
  },
  mounted() {
    this.getRentComment();
  },
  methods: {
    //获取租车的评论数据
    getRentComment() {
      axios.get("/comments/getRent", { params: { status: "" } }).then(res => {
        const nt = res.data.filter(item => {
          return item.status == 0;
        });
        // console.log(this.negative);
        //获取长度
        this.negativeLength = nt.length;
        // console.log(this.negativeLength);
        const no = res.data.filter(item => {
          return item.status == 1;
        });
        // console.log(this.noComment);
        //获取长度
        this.noCommentLength = no.length;

        const good = res.data.filter(item => {
          return item.status == 2;
        });
        // console.log(this.goodComment);
        //获取长度
        this.goodCommentLength = good.length;
        //引入统计图
        this.chartRent();
      });
    },
    //租车评价的统计图
    chartRent() {
      // 基于准备好的dom，初始化echarts实例
      var chart = this.$echarts.init(document.getElementById("rent"));
      chart.setOption({
        backgroundColor: "#2c343c",

        title: {
          text: "评价统计图",
          left: "center",
          top: 20,
          textStyle: {
            color: "#ccc"
          }
        },
        tooltip: {
          trigger: "item",
          formatter: "{a} <br/>{b} : {c} ({d}%)"
        },

        // visualMap: {
        //   show: false,
        //   min: 80,
        //   max: 600,
        //   inRange: {
        //     colorLightness: [90, 100]
        //   }
        // },
        series: [
          {
            name: "访问来源",
            type: "pie",
            radius: "55%",
            center: ["50%", "50%"],
            data: [
              { value: this.negativeLength, name: "差评" },
              { value: this.noCommentLength, name: "未评论" },
              { value: this.goodCommentLength, name: "好评" }
            ].sort(function(a, b) {
              return a.value - b.value;
            }),
            roseType: "radius",
            label: {
              color: "rgba(255, 255, 200)"
            },
            labelLine: {
              lineStyle: {
                color: "rgba(255, 255, 200)"
              },
              smooth: 0.2,
              length: 10,
              length2: 20
            },
            itemStyle: {
              color: "#c23531",
              shadowBlur: 200,
              shadowColor: "rgba(0, 0, 0, 0.5)"
            },

            animationType: "scale",
            animationEasing: "elasticOut",
            animationDelay: function(idx) {
              return Math.random() * 200;
            }
          }
        ]
      });
    }
  }
};
</script>

<style>
#rent {
  width: 100%;
  height: 600px;
}
</style>